/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.entity.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import org.ipti.br.tag.bean.lazy.LazyHumanDataModel;
import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.AdditionalInfoBO;
import org.ipti.br.tag.bo.HumanBO;
import org.ipti.br.tag.bo.PersonageBO;
import org.ipti.br.tag.bo.UnityBO;

/**
 *
 * @author root
 */
@ManagedBean(name = "actorForHuman")
@ViewScoped
public class ActorForHumanBean extends AppBaseBean {

    @Inject
    private ActorBO actorBO;
    @Inject
    private AdditionalInfoBO additionalInfoBO;
    @Inject
    private HumanBO humanBO;
    @Inject
    private PersonageBO personageBO;
    @Inject
    private UnityBO unityBO;
    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;
    private Actor actor;
    private Human human;
    private Long personageID;
    private Long unityForRegisterID;
    private boolean deactivatedIsNull;
    private List<SelectItem> unitiesForRegister;
    private LazyHumanDataModel humanList;

    /** Creates a new instance of ActorForHumanBean */
    public ActorForHumanBean() {
    }

    public void initialize() {
        unityForRegisterID = null;
        actor = new Actor();
        actor.setActivated(Calendar.getInstance());
        actor.setDeactivated(Calendar.getInstance());
        human = new Human();
        humanList = new LazyHumanDataModel(humanBO.getHumanDAO(), null);
        deactivatedIsNull = true;
    }

    public String getPersonageName() {
        return getPersonageBO().find(personageID).getName();
    }

    public List<AdditionalInfo> personAdditionalInfo(Long personID) {
        return additionalInfoBO.findAddInfosPerPerson(personID);
    }

    public void loadUnitiesForRegister() {
        unitiesForRegister = new ArrayList<SelectItem>();
        unitiesForRegister.add(new SelectItem(null, "Selecione um local"));
        for (Unity u : getUnityBO().unityChildrenPerOrganization(sessionBean.getLoggedActor().getUnity(),
                getPersonageBO().find(personageID).getOrganization())) {
            unitiesForRegister.add(new SelectItem(u.getId(), u.getName()));
        }
    }

    public void createActor() {
        if (human != null) {
            try {

                Personage p = getPersonageBO().find(personageID);
                Unity u = getUnityBO().find(unityForRegisterID);
                actor.setPersonage(p);
                actor.setUnity(u);
                actor.setPerson(human);

                if (deactivatedIsNull) {
                    actor.setDeactivated(null);
                }
                getActorBO().create(actor);


                initialize();
                addInfoMessage("actorForHumanForm", "Cadastro realizado com sucesso.", null);
            } 
            catch (Exception ex) {                
                addErrorMessage("actorForHumanForm", "Um erro foi encontrado", null);
                                
            }
        } else {
            addErrorMessage("actorForHumanForm", "Escolha um humano", null);
        }
    }

    /*
     * Getters and Setters
     */
    public ActorBO getActorBO() {
        return actorBO;
    }

    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }

    public AdditionalInfoBO getAdditionalInfoBO() {
        return additionalInfoBO;
    }

    public void setAdditionalInfoBO(AdditionalInfoBO additionalInfoBO) {
        this.additionalInfoBO = additionalInfoBO;
    }

    public HumanBO getHumanBO() {
        return humanBO;
    }

    public void setHumanBO(HumanBO humanBO) {
        this.humanBO = humanBO;
    }

    public PersonageBO getPersonageBO() {
        return personageBO;
    }

    public void setPersonageBO(PersonageBO personageBO) {
        this.personageBO = personageBO;
    }

    public UnityBO getUnityBO() {
        return unityBO;
    }

    public void setUnityBO(UnityBO unityBO) {
        this.unityBO = unityBO;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }

    public Human getHuman() {
        return human;
    }

    public void setHuman(Human human) {
        this.human = human;
    }

    public Long getPersonageID() {
        return personageID;
    }

    public void setPersonageID(Long personageID) {
        this.personageID = personageID;
    }

    public Long getUnityForRegisterID() {
        return unityForRegisterID;
    }

    public void setUnityForRegisterID(Long unityForRegisterID) {
        this.unityForRegisterID = unityForRegisterID;
    }

    public boolean isDeactivatedIsNull() {
        return deactivatedIsNull;
    }

    public void setDeactivatedIsNull(boolean deactivatedIsNull) {
        this.deactivatedIsNull = deactivatedIsNull;
    }

    public List<SelectItem> getUnitiesForRegister() {
        if (unitiesForRegister == null) {
            loadUnitiesForRegister();
        }
        return unitiesForRegister;
    }

    public void setUnitiesForRegister(List<SelectItem> unitiesForRegister) {
        this.unitiesForRegister = unitiesForRegister;
    }

    public LazyHumanDataModel getHumanList() {
        return humanList;
    }

    public void setHumanList(LazyHumanDataModel humanList) {
        this.humanList = humanList;
    }
}
